package com.ikkong.shine.tools;

import java.sql.SQLException;
import java.util.List;

import com.ikkong.common.config.MainConfig;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.toolbox.Record;
import com.ikkong.shine.model.Doctor;
import com.ikkong.shine.model.QueueType;
import com.ikkong.shine.model.Rlt_doctor2queue_type;
import com.ikkong.shine.service.DataConnectionService;
import com.ikkong.shine.service.DbSqlService;
import com.ikkong.shine.service.impl.DataConnectionServiceImpl;
import com.ikkong.shine.service.impl.DbSqlServiceImpl;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.druid.DruidPlugin;

public class GetDataSouceConn {
	static DataConnectionService dataconnectionservice = new DataConnectionServiceImpl();
	DbSqlService dbsqlservice = new DbSqlServiceImpl();
	Log log = Log.getLog(GetDataSouceConn.class);
	Exception exception = new SQLException();
	Exception e = new Exception();
	private static String sqltodoctor = null;// 医生
	private static String sqltoqueutype = null;// 队列
	private static String sqltodoctorforqueuetype = null;// 医生与队列

	// 获取医生信息
	public void getDoctorOrQueueType() {
		// 查询数据源连接
		Record record = dataconnectionservice.dataConnection();
		if (null == record) {
			return;
		}
		String id = record.getStr("id");
		DruidPlugin druidPlugin = null;
		ActiveRecordPlugin arp = null;
		if (!MainConfig.dbconfigs.contains(id)) {
			String driver = record.getStr("driver");
			String url = record.getStr("url");
			String username = record.getStr("username");
			String password = record.getStr("password");

			
			druidPlugin = new DruidPlugin(url, username, password.trim(),
					driver);
			arp = new ActiveRecordPlugin(id, druidPlugin);
			druidPlugin.start();
			arp.start();
			MainConfig.dbconfigs.add(id);

			List<Record> list = dbsqlservice.findDbSql();// 查询数据项信息dbsql表
			for (Record rd : list) {
				switch (rd.getInt("type")) {
				case 1:
					sqltodoctor = rd.getStr("sqlstring");// 医生信息
					break;
				case 2:
					sqltoqueutype = rd.getStr("sqlstring");// 队列信息
					break;
				case 3:
					sqltodoctorforqueuetype = rd.getStr("sqlstring");// 排班信息
					break;
				default:
					log.error(e.getMessage());
					break;
				}
			}
		}
		if (StrKit.notBlank(sqltodoctor)) {
			List<com.jfinal.plugin.activerecord.Record> recordList = null;

			recordList = Db.use(id).find(sqltodoctor);
			long sumCount = 0;
			long suCount = 0;
			if (null != recordList) {
				sumCount = recordList.size();
				for (com.jfinal.plugin.activerecord.Record record2 : recordList) {
					try {
						Blade.create(Doctor.class).save(record2);
						suCount++;
					} catch (Exception e) {
						log.error("医生信息同步出错" + e.getMessage());
					}
				}
				log.error("Doctor信息同步完成。总共查询到：" + sumCount + "条数据。成功插入："
						+ suCount + "条");
			}

		}
		if (StrKit.notBlank(sqltoqueutype)) {
			List<com.jfinal.plugin.activerecord.Record> recordList = null;

			recordList = Db.use(id).find(sqltoqueutype);
			long sumCount = 0;
			long suCount = 0;
			if (null != recordList) {
				sumCount = recordList.size();
				for (com.jfinal.plugin.activerecord.Record record2 : recordList) {
					QueueType qt = new QueueType();
					try {
						qt.setSource_id(record2.getStr("source_id"));
						qt.setName(record2.getStr("name"));
						qt.setDisplayname(record2.getStr("displayname"));
						qt.setIs_checkin(0);
						qt.setIs_pretriage(0);
						qt.setIs_reorder(0);
						qt.setIs_reserve(0);
						qt.setAvg_minute(0);
						qt.setReserve_numlist("");
						Blade.create(QueueType.class).save(qt);
						suCount++;
					} catch (Exception e) {
						log.error("出错数据：" + qt.toString());
					}
				}
				log.error("QueueType信息同步完成。总共查询到：" + sumCount + "条数据。成功插入："
						+ suCount + "条");
			}
		}
		if (StrKit.notBlank(sqltodoctorforqueuetype)) {
			List<com.jfinal.plugin.activerecord.Record> recordList = null;
			recordList = Db.use(id).find(sqltodoctorforqueuetype);
			long sumCount = 0;
			long suCount = 0;
			if (null != recordList) {
				sumCount = recordList.size();
				for (com.jfinal.plugin.activerecord.Record record2 : recordList) {
					Rlt_doctor2queue_type rltdoctor2queuetype = new Rlt_doctor2queue_type();
					try {
						rltdoctor2queuetype.setDoctor_id(record2
								.getInt("doctor_id"));
						rltdoctor2queuetype.setQueue_type_id(record2
								.getInt("queue_type_id"));
						rltdoctor2queuetype.setOnduty(record2.getStr("onduty"));
						Blade.create(Rlt_doctor2queue_type.class).save(
								rltdoctor2queuetype);
						suCount++;
					} catch (Exception e) {
						log.error("出错数据：" + rltdoctor2queuetype.toString());
					}
				}

				log.error("Rlt_Doctor_QueueType信息同步完成。总共查询到：" + sumCount
						+ "条数据。成功插入：" + suCount + "条");
			}
		}
		
		arp.stop();
		druidPlugin.stop();
	}
}
